<html>
<head><meta charset="utf-8"><title>Homogenous conversion of Error types and trait objects · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html">Homogenous conversion of Error types and trait objects</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="165339079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165339079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165339079">(May 10 2019 at 14:12)</a>:</h4>
<p><a href="#narrow/stream/122651-general/topic/.26Box.3Cdyn.20Error.3E.20can't.20be.20.26dyn.20Error" title="#narrow/stream/122651-general/topic/.26Box.3Cdyn.20Error.3E.20can't.20be.20.26dyn.20Error">previously...</a></p>
<p>I have a procedural macro that wants to implement <code>Error</code> for an enum containing different types of underlying source errors (<a href="https://docs.rs/snafu/0.3.0/snafu/" target="_blank" title="https://docs.rs/snafu/0.3.0/snafu/">SNAFU</a>). I keep running into a problem with the differences of trait objects and concrete objects when converting them to a reference trait object:</p>
<div class="codehilite"><pre><span></span><span class="k">enum</span> <span class="nc">Error</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">TraitObjectSendSync</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">error</span>::<span class="n">Error</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Sync</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">TraitObjectSend</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">error</span>::<span class="n">Error</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">TraitObjectSync</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">error</span>::<span class="n">Error</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Sync</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">TraitObject</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="n">error</span>::<span class="n">Error</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Boxed</span><span class="p">(</span><span class="nb">Box</span><span class="o">&lt;</span><span class="n">io</span>::<span class="n">Error</span><span class="o">&gt;</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Unboxed</span><span class="p">(</span><span class="n">io</span>::<span class="n">Error</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span><span class="w">    </span><span class="k">fn</span> <span class="nf">source</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;&amp;</span><span class="p">(</span><span class="n">dyn</span><span class="w"> </span><span class="n">error</span>::<span class="n">Error</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">&#39;static</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">use</span><span class="w"> </span><span class="n">Error</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>

<span class="w">        </span><span class="c1">// Works, but the match arms are heterogeneous and cannot be</span>
<span class="w">        </span><span class="c1">// automatically determined / generated via a procedural macro</span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="o">*</span><span class="bp">self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">TraitObjectSendSync</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">&amp;**</span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">TraitObjectSend</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">&amp;**</span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">TraitObjectSync</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">&amp;**</span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">TraitObject</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="o">&amp;**</span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Boxed</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Unboxed</span><span class="p">(</span><span class="k">ref</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>The <a href="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=28a8ffd874c8b4f1e546fd0d068bc1d3" target="_blank" title="https://play.integer32.com/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=28a8ffd874c8b4f1e546fd0d068bc1d3">playground</a> has various attempts I've made: </p>
<ul>
<li><code>=&gt; e</code></li>
<li><code>=&gt; e as &amp;(dyn error::Error + 'static)</code></li>
<li><code>Borrow::borrow(e)</code></li>
<li><code>Deref::deref(e)</code></li>
<li><code>AsRef::as_ref(e)</code></li>
</ul>
<p>None work. Is there <strong>any</strong> way I can have this work without requiring users to annotate if something is a trait object?</p>



<a name="165339569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165339569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165339569">(May 10 2019 at 14:18)</a>:</h4>
<p>In 2018 I guess maybe looking for <code>dyn Foo</code> in the type?</p>



<a name="165339575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165339575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165339575">(May 10 2019 at 14:18)</a>:</h4>
<p>as a heuristic, anyway</p>



<a name="165340111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340111">(May 10 2019 at 14:24)</a>:</h4>
<p>I'd be pretty sad about a heuristic :-(</p>



<a name="165340243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340243">(May 10 2019 at 14:26)</a>:</h4>
<p>I wonder why <code>&amp;*e</code> does not work</p>
<div class="codehilite"><pre><span></span>error[E0277]: the size for values of type `(dyn std::error::Error + &#39;static)` cannot be known at compilation time
</pre></div>


<p>why does it even get that as an rvalue?</p>



<a name="165340279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340279">(May 10 2019 at 14:27)</a>:</h4>
<p>oh, probably because it uses some <code>impl&lt;T&gt; Error for Box&lt;T&gt; where T: Error</code></p>



<a name="165340298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340298">(May 10 2019 at 14:27)</a>:</h4>
<p>I believe that's in the right area, <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="165340300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340300">(May 10 2019 at 14:27)</a>:</h4>
<p>is that impl a thing? can we make it <code>T: ?Sized</code>?</p>



<a name="165340379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340379">(May 10 2019 at 14:28)</a>:</h4>
<p>(<a href="#narrow/stream/122651-general/topic/.26Box.3Cdyn.20Error.3E.20can't.20be.20.26dyn.20Error" title="#narrow/stream/122651-general/topic/.26Box.3Cdyn.20Error.3E.20can't.20be.20.26dyn.20Error">You had that idea already ;-)</a>)</p>



<a name="165340395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340395">(May 10 2019 at 14:28)</a>:</h4>
<p><em>lol</em></p>



<a name="165340402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340402">(May 10 2019 at 14:28)</a>:</h4>
<p>at least I am consistent :P</p>



<a name="165340432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165340432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165340432">(May 10 2019 at 14:29)</a>:</h4>
<p>that wouldnt give you the trait object you want (no need to carry that extra <code>Box</code> around), but I guess if its only for errors anyway the extra ptr indirection is not so bad</p>



<a name="165341465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165341465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165341465">(May 10 2019 at 14:40)</a>:</h4>
<p>I feel like I want "deref a value until you get to a trait and then take a reference". That kind of structure always leads to either auto-traits or specialization, which isn't a good road for stable stuff.</p>



<a name="165355817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165355817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165355817">(May 10 2019 at 17:43)</a>:</h4>
<p>The wonderful <span class="user-mention" data-user-id="125254">@kennytm</span> <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=538fbae7dde34d32b71d4d6a7e15d74a" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=538fbae7dde34d32b71d4d6a7e15d74a">solved it for me with a custom trait</a> and relying on method auto-deref.</p>



<a name="165356461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165356461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165356461">(May 10 2019 at 17:52)</a>:</h4>
<p>nice!</p>



<a name="165356478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165356478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165356478">(May 10 2019 at 17:52)</a>:</h4>
<p>still might want to add that <code>T: ?Sized</code> to whatever impl doesnt have it, though^^</p>



<a name="165467889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165467889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165467889">(May 12 2019 at 14:54)</a>:</h4>
<p>reported as <a href="https://github.com/rust-lang/rust/issues/60759" target="_blank" title="https://github.com/rust-lang/rust/issues/60759">https://github.com/rust-lang/rust/issues/60759</a> (I couldnt find an existing report)</p>



<a name="165492028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Homogenous%20conversion%20of%20Error%20types%20and%20trait%20objects/near/165492028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Homogenous.20conversion.20of.20Error.20types.20and.20trait.20objects.html#165492028">(May 13 2019 at 02:07)</a>:</h4>
<p>Thank you! I just tried to add the implementation, saw there were errors, and said "oh, I guess it can't exist"</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>